package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.WritDetails;
import com.jiantong.lawEnforcing.service.WritDetailsService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 文书存储表
 * Created by mz03 on 2017/8/25.
 */
@Slf4j
@OpenBean
public class WritDetailsApis {
    @RpcConsumer
    private WritDetailsService writDetailsService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.writDetails.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public WritDetails findById(Long id){
        Response<WritDetails> response = writDetailsService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find news read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param writDetails
     * @return
     */
    @OpenMethod(
            key = "create.writDetails",
            paramNames = {"writDetails"},
            httpMethods = RequestMethod.POST
    )
    public Long create(WritDetails writDetails){
        Response<Long> response = writDetailsService.create(writDetails);
        if(!response.isSuccess()){
            log.error("failed to create writDetails read by writDetails = {}, cause : {}", writDetails, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.writDetails",
            paramNames = {"writDetails"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(WritDetails writDetails){
        if(writDetails.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = writDetailsService.update(writDetails);
        if(!response.isSuccess()){
            log.error("failed to update writDetails");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update writDetails");
        }
        return response.getResult();
    }

    /**
     * 根据类型查询文书  分页
     * @param pageNo
     * @param pageSize
     * @param cases_basic_id
     * @param type
     * @return
     */
    @OpenMethod(
            key = "find.paging.writDetails",
            paramNames = {"pageNo","pageSize","cases_basic_id","type"},
            httpMethods = RequestMethod.GET
    )
    public Paging<WritDetails> pagingCasesBasic(
            Integer pageNo,
            Integer pageSize,
            Long cases_basic_id,
            Integer type
    ){
        Response<Paging<WritDetails>> pagingResponse =writDetailsService.findByType(pageNo,pageSize,cases_basic_id,type);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging WritDetails type {}, cause : {}", type, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

}
